#source("http://lu-library.googlecode.com/svn/trunk/util.r")
require(Matrix)
require(tseries)

L1normalize <- function(row_vector) {
	divider = sum(row_vector)
	if(divider == 0) {
		return(row_vector)
	}
	else {
		return(row_vector/divider)
	}	
}

object.sizes <- function() {
	#return the size of all variables
    return(rev(sort(sapply(ls(envir=.GlobalEnv), function (object.name) 
        object.size(get(object.name))))))
}


ZeroOneNormalize <- function(rowvector) {
	maxv = max(rowvector)
	minv = min(rowvector)
	if(maxv == minv)	return(rowvector)
	else return((rowvector-minv)/(maxv-minv))
}


L2normalize <- function(row_vector) {
	divider = sqrt(sum(row_vector^2))
	if (divider == 0) {
		return(row_vector)
	}
	else {
		return(row_vector/divider)
	}	
}


LoadSpbof <- function(infile, dim) {
	txt <- read.delim(infile, header = FALSE)
	txt <- as.character(txt[1,1])
	tokens <- strsplit(txt, split=" ")[[1]]
	result <- vector(mode = "numeric", length = dim)
	for(i in 1:length(tokens)) {
		if(tokens[i]=="") next
		temp <- strsplit(tokens[i], split=":")[[1]]
		result[as.numeric(temp[1])] = temp[2]
	}
	return(result)
}


LoadSpbof_Java <- function(infile, dim) {
	tmpfile = paste(infile, ".matrix", sep="")
	command1 = paste("java -jar svm2matrix.jar ", infile, " ", tmpfile, " ", dim, sep="")
	system(command1, show.output.on.console=FALSE)
	
	result = read.matrix(tmpfile)
	
	file.remove(tmpfile)
	return(result)
	#if(Sys.info()[1]=="Windows") {
	#	command2 = paste("del ", tmpfile, sep="")
	#	command2 = gsub("/", "\\\\", command2)	
	#} else {
	#	command2 = paste("rm ", tmpfile, sep="")
	#}
	#system(command2, show.output.on.console=FALSE)
}

ToLibSVMBoF <- function(rowvector) {
	idx = which(rowvector != 0)
	spbof = paste("", paste(paste(idx, rowvector[idx], sep=":"), collapse = " "))
	return(spbof)
}



calculateKernelMatrix <- function(featmat1, featmat2, FUN) {
	#input is two feature matrices with n*p, m*p
	#return a n*m kernel matrix. FUN specify the kernel function
	kernelMatrix = matrix(0, nrow = nrow(featmat1), ncol = nrow(featmat2))
	for(i in 1:nrow(kernelMatrix)) {
		for(j in 1:ncol(kernelMatrix)) {
			kernelMatrix[i,j] = FUN(featmat1[i,], featmat2[j,])
		}
	}
	return(kernelMatrix)
}



calculateSquareKernelMatrix <- function(featmatrix, FUN) {
	#more efficient
	#input is a feature matrix with n*m
	#return a n*n kernel matrix. FUN specify the kernel function
	kernelMatrix = matrix(0, nrow = nrow(featmatrix), ncol = nrow(featmatrix))
	for(i in 1:nrow(featmatrix)) {
		for(j in 1:(i-1)) {
			kernelMatrix[i,j] = FUN(featmatrix[i,], featmatrix[j,])
		}
		kernelMatrix[i,i] = 1	#self-value
	}
	
	for(i in 1:nrow(featmatrix)) {
		for(j in i:nrow(featmatrix)) {
			kernelMatrix[i,j] = kernelMatrix[j,i]
		}
	}
	return(kernelMatrix)
}

IntersectionKernel <- function(x, y) {
	return(sum(pmin(x,y)))
}

Chi2Kernel <- function(x, y) {
	z = ((x-y)^2)/(x+y)
	z[is.infinite(z)] = 0
	return(sum(z))
}